package edu.zzuli.dao;

import edu.zzuli.pojo.User;
import edu.zzuli.utils.DbUtil;
import edu.zzuli.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import sun.jdbc.odbc.ee.DataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Chen-D.W on 2017/12/25.
 */
public class RegeistDao {
    /**
     * 插入
     */
    public void insert(User user) throws Exception {

//        Connection connection=null;
//        PreparedStatement preparedStatement=null;
//        ResultSet resultSet=null;
//        String sql="insert INTO  user(username,password,mail,status,code) VALUES (?,?,?,?,?)";
//        connection=DbUtil.getConnection();
//        preparedStatement=connection
//                .prepareStatement(sql);
//
//        preparedStatement.setString(1,user.getUsername());
//
//        preparedStatement.setString(2,user.getPassword());
//
//        preparedStatement.setString(3,user.getMail());
//
//        preparedStatement.setInt(4,user.getStatus());
//
//        preparedStatement.setString(5,user.getCode());
//
//        preparedStatement.execute();
//
//        new DbUtil().close(connection,preparedStatement,resultSet);

        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="insert INTO  user(username,password,mail,status,code) VALUES (?,?,?,?,?)";
        Object [] params={user.getUsername(),user.getPassword(),user.getMail(),user.getStatus(),user.getCode()};
        queryRunner.update(sql,params);
    }
    /**
     * 验证
     */
    public User check(String code) throws SQLException {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        String sql="select * FROM user WHERE code=?";
        connection=new DbUtil().getConnection();

        preparedStatement=connection.prepareStatement(sql);

        preparedStatement.setString(1,code);



        resultSet=preparedStatement.executeQuery();

        User user=new User();

        while (resultSet.next()){
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            user.setMail(resultSet.getString("mail"));
            user.setStatus(resultSet.getInt("status"));
            user.setCode(resultSet.getString("code"));
            user.setId(resultSet.getLong("id"));
        }

//        new DbUtil().close(connection,preparedStatement,resultSet);

        return user;
    }

    public static void update(User user) throws SQLException {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        String sql="update  USER SET username='"+user.getUsername()+"',password='"+user.getPassword()
                +"',mail='"+user.getMail()+"',status='"+user.getStatus()+"',code='"+user.getCode()+"' WHERE id="+user.getId()+"";
        connection=new DbUtil().getConnection();
        preparedStatement=connection.prepareStatement(sql);
        preparedStatement.executeUpdate();
//        preparedStatement=connection.prepareStatement(sql);
//
//        preparedStatement.setString(1,user.getUsername());
//        preparedStatement.setString(2,user.getPassword());
//        preparedStatement.setString(3,user.getMail());
//        preparedStatement.setInt(4,user.getStatus());
//        preparedStatement.setString(5,user.getCode());
//        preparedStatement.setLong(6,user.getId());
//
//
//
//
//        preparedStatement.executeUpdate();

//        new DbUtil().close(connection,preparedStatement,resultSet);

    }
}
